﻿using MESPro.DBManager;
using MESPro.Enums;
using MESPro.Global;
using MESPro.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;

namespace MESPro.Views
{
    public partial class FunctionListFrm : DockContent
    {

        #region Public Constructors

        public FunctionListFrm()
        {
            InitializeComponent();
        }

        #endregion Public Constructors

        #region 字段

        private List<Function>? modelList;

        #endregion 字段

        #region 属性

        /// <summary>
        /// ListView选择项改变后触发
        /// </summary>
        public Action<ListViewItem?>? RMListViewItemSelected { get; set; }
        /// <summary>
        /// 提交到数据库后,需要刷新界面
        /// </summary>
        public Action? RMUpdateableByObject { get; set; }
        /// <summary>
        /// 发生异常后触发的动作
        /// </summary>
        public Action<object, MsgLevel>? RMShowMsg { get; set; }
        /// <summary>
        /// 所有的模块
        /// </summary>
        public List<Function>? RMModelList
        {
            get => modelList;
            set
            {
                modelList = value;
                this.Invoke(() => { 等到数据后刷新界面(); });
            }
        }

        #endregion 属性

        #region Public 方法

        /// <summary>
        /// 等到数据后刷新界面
        /// </summary>
        public void 等到数据后刷新界面()
        {
            this.listView1.Clear();
            if (this.RMModelList == null) return;
            foreach (Function item in RMModelList)
            {
                ListViewItem listViewItem1
                    = new ListViewItem(item.DisLanguage.GetDisplayName(), item.ImageKey);
                listViewItem1.Tag = item;
                this.listView1.Items.Add(listViewItem1);
            }
        }

        #endregion Public 方法

        #region Private 方法

        private void button1_Click(object sender, EventArgs e)
        {
            FunctionManager modelManager = new FunctionManager();
            try
            {
                //1.

                var res = modelManager.AddFunction(new Function()
                {
                    DisLanguage = new DisLanguage() { zh_CN = $"功能{Guid.NewGuid()}" }
                });

                RMShowMsg?.Invoke(res, MsgLevel.Trace);
                RMUpdateableByObject?.Invoke();
                RMListViewItemSelected?.Invoke(null);
            }
            catch (Exception ex)
            {

                throw;
            }

        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (this.listView1.SelectedItems == null)
            {
                RMShowMsg?.Invoke("请选择要执行得项", MsgLevel.Trace);
                return;
            }
            if (this.listView1.SelectedItems.Count < 1)
            {
                RMShowMsg?.Invoke("请选择要执行得项", MsgLevel.Trace);
                return;
            }
            if (this.listView1.SelectedItems[0].Tag == null)
            {
                RMShowMsg?.Invoke("请选择要执行得项", MsgLevel.Trace);
                return;
            }
            var obj = this.listView1.SelectedItems[0].Tag as Function;

            //导航删除
            if (obj is null)
            {
                RMShowMsg?.Invoke("obj转成function失败", MsgLevel.Trace);
                return;
            }
            var res = DbContext.Db.DeleteNav<Function>(it => it.Id == obj.Id)
                        .Include(z1 => z1.DbControlGs)
                        .ExecuteCommand();
            // var res = DbContext.Db.DeleteableByObject(obj).ExecuteCommand();
            RMShowMsg?.Invoke(res?"删除成功":"删除失败", MsgLevel.Trace);
            RMUpdateableByObject?.Invoke();
            RMListViewItemSelected?.Invoke(null);
        }

        private void 选择项改变(object sender, ListViewItemSelectionChangedEventArgs e)
        {
            if (this.listView1.SelectedIndices.Count > 0)
            {
                RMListViewItemSelected?.Invoke(e.Item);
            }
        }

        private async void button3_Click(object sender, EventArgs e)
        {
            if (this.listView1.SelectedItems == null)
            {
                RMShowMsg?.Invoke("请选择要执行得项", MsgLevel.Trace);
                return;
            }
            if (this.listView1.SelectedItems.Count < 1)
            {
                RMShowMsg?.Invoke("请选择要执行得项", MsgLevel.Trace);
                return;
            }
            if (this.listView1.SelectedItems[0].Tag == null)
            {
                RMShowMsg?.Invoke("请选择要执行得项", MsgLevel.Trace);
                return;
            }
            var obj = this.listView1.SelectedItems[0].Tag as Function;
            if (obj == null) return;
            obj.Id = 0;
            obj.DisName = obj.DisName + "复制";
            Stopwatch stopwatch = Stopwatch.StartNew();
            List<Function> list = new List<Function>() { obj };
            var res = await Task.Run(() =>
            {
                return DbContext.Db.InsertNav(list).Include(it => it.DbControlGs).ExecuteCommand();

            });

            Debug.WriteLine(stopwatch.ElapsedMilliseconds);
            RMShowMsg?.Invoke(res, MsgLevel.Trace);
            Debug.WriteLine(stopwatch.ElapsedMilliseconds);

            RMUpdateableByObject?.Invoke();
            Debug.WriteLine(stopwatch.ElapsedMilliseconds);

            RMListViewItemSelected?.Invoke(null);
            Debug.WriteLine(stopwatch.ElapsedMilliseconds);

        }

        #endregion Private 方法
    }
}
